﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TuChongPicView.Common;
using TuChongPicView.Entity;
using Image = TuChongPicView.Entity.Image;

namespace TuChongPicView
{
    /// <summary>
    /// ImgView.xaml 的交互逻辑
    /// </summary>
    public partial class ImgView : Window
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private BlockingCollection<Post> blockingCollection = null;
        System.Timers.Timer timer = new System.Timers.Timer();
        private Post post = null;
        private int imgIndex = 0;

        public delegate void NeedAddImg();
        public event NeedAddImg NeedAddImgEvent;
        public delegate void ExitView();
        public event ExitView ExitViewEvent;

        public ImgView(BlockingCollection<Post> blockingCollection)
        {
            InitializeComponent();
            this.blockingCollection = blockingCollection;
            this.Visibility = Visibility.Hidden;
            timer.Interval = Constants.timeInterval;
            timer.Elapsed += Timer_Elapsed;
            timer.AutoReset = true;

            this.imgUrl.Click += ImgUrl_Click;
            this.Exit.MouseDown += Exit_MouseDown;
        }

        private void Exit_MouseDown(object sender, MouseButtonEventArgs e)
        {
            log.Debug("退出展示");
            timer.Stop();
            ExitViewEvent();
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            log.Debug("定时器执行");
            log.Debug("缓存队列数量：" + blockingCollection.Count);
            if (blockingCollection.Count <= 5 )
            {
                log.Debug("发送 NeedAddImgEvent");
                NeedAddImgEvent();
            }
            this.Dispatcher.Invoke(new Action(delegate {
                showImg();
            }));
        }

        public void showImg()
        {
            log.Debug("ShowImg执行");
            if (post == null) {
                post = blockingCollection.Take();
                log.Debug("取出POST");
            }
            if (post != null) {
                List<Image> images = post.images;
                if (images != null && images.Count > 0) {
                    Image image = images[imgIndex];
                    this.title.Text = image.title==""? post.title: image.title;
                    //this.userName.Text = post.published_at;
                    this.imgUrl.NavigateUri = new Uri(post.url);
                    this.img.ImageSource = new BitmapImage(new Uri("pack://SiteOfOrigin:,,,/pic/" + image.localFileName));
                    this.imgUrlText.Text = post.url;

                    if (imgIndex == (images.Count-1))
                    {
                        post = blockingCollection.Take();
                        imgIndex = 0;
                    }
                    else {
                        imgIndex++;
                    }
                }
            }
            
            //this.title.Text = 
        }

        private void ImgUrl_Click(object sender, RoutedEventArgs e)
        {
            Hyperlink link = sender as Hyperlink;
            Process.Start(new System.Diagnostics.ProcessStartInfo(link.NavigateUri.AbsoluteUri));
        }

        public void startTimer() {
            timer.Enabled = true;
            timer.Start();
        }

        public void ClearQueue() {
            this.post = null;
            this.imgIndex = 0;
            int count = blockingCollection.Count;
            if (blockingCollection != null && count > 0)
            {
                blockingCollection.Take(count);
            }
        }
    }
}
